<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

chdir("/home/cm/public_html/crons/");
include('../config.php');

$er = new Email_reader(EMAIL_TASKS_SERVER, EMAIL_TASKS_USER, EMAIL_TASKS_PASSWORD);

$inbox = $er->inbox('');

//base_obj::pretty_var_dump(count($inbox));

if (!empty($inbox)) {
    foreach ($inbox as $i => $message) {

        $index = $message['index'];
        $header_array = json_decode(json_encode($message['header']), true);
        $header_array['date_timestamp'] = strtotime($header_array['Date']);
        $body = $message['body'];
        $email_hash = md5($header_array['date_timestamp'] . $header_array['message_id'] . $header_array['subject']);
        $domain = $header_array['from'][0]['host'];
        $description = 'From: ' . $header_array['from'][0]['mailbox'] . '@' . $header_array['from'][0]['host'] . ': ' . $body;

        if ($email_hash == $_REQUEST['hash']) {
            base_obj::pretty_var_dump($index);
            base_obj::pretty_var_dump($header_array);
            base_obj::pretty_var_dump($body);
            base_obj::pretty_var_dump($domain);
            base_obj::pretty_var_dump($email_hash);
            base_obj::pretty_var_dump($description);

            break;
        }
    }
}

//$task_obj->addLastEmailProcessing();
?>
